#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import os

from ctools.converters.convbase import NConverterFilterBase
from ctools.datahelper.functree import SNode
from ctools.ly_parser.nodes import CTreeNode
from ctools.datahelper.texthelper import *

from ctools.logext import *
import logging

_rlog, _plog, _mlog, _tlog = get_loggers(__file__, __name__, propagate=False)

class ConvSN2C(NConverterFilterBase):
    def process(self, nd:SNode, indent=1, **kwargs):
        ctrl_direction = kwargs.get('ctrl_direction', '')
        sequence = kwargs.get('sequence', True)

        def _func(node, args, depth):
            if depth == 0:
                return
            if depth == 1:
                if not node.parent.name.strip():
                    return
            if not self._filter_item(node.name):
                _mlog.debug(f'skip item: {node}')
                return

            if args['direction'] == '>':
                line = f"{node.parent.name} > {node.name}"
            else:
                line = f"{node.name} > {node.parent.name}"
            args['lines'].append(line)

        args = {
            'lines': [],
            'direction': nd._direction,
        }
        self.iter_nodes(_func, args)
        # FIXME:
        return '    \n'.join(args['lines'])

    convert = process

class ConvSN2Gv(NConverterFilterBase):

    def process(self, nd:SNode, indent=1, **kwargs) -> str:
        ctrl_direction = kwargs.get('ctrl_direction', '')
        sequence = kwargs.get('sequence', True)

        def _func(node, args, depth):
            if depth == 0:
                return
            if depth == 1:
                if not node.parent.name.strip():
                    return
            if not self._filter_item(node.name):
                _mlog.debug(f'skip item: {node}')
                return

            if args['direction'] == '>':
                line = f'{node.parent.name} -> {node.name};'
            else:
                line = f'{node.name} -> {node.parent.name};'
            args['lines'].append(line)

        args = {
            'lines': [],
            'direction': nd.direction,
        }
        nd.iter_nodes(_func, args)
        # FIXME:
        text =  '    \n'.join(args['lines'])
        return text

    convert = process
